package com.crm.system.controller;

import com.crm.dto.PartnerCount;
import com.crm.entity.Role;
import com.crm.model.JsonMessage;
import com.crm.security.AuthUserDetail;
import com.crm.system.service.SysRoleService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@Api(value = "角色接口")
@RequestMapping("/user/role")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    @ApiOperation(value = "伙伴统计")
    @GetMapping("partnerCount")
    @ResponseBody
    public JsonMessage partnerCount(Authentication authentication, String beginTime, String endTime,
                                    @RequestParam(defaultValue = "1") int pageIndex,
                                    @RequestParam(defaultValue = "10") int pageSize) {
        JsonMessage message = new JsonMessage();
        AuthUserDetail detail = (AuthUserDetail) authentication.getDetails();
        if ("004".equals(detail.getRoleId())) {
            PageInfo<PartnerCount> count = sysRoleService.partnerCount(sysRoleService.depId(detail.getUserId()), beginTime, endTime, pageIndex, pageSize);
            message.setData(count);
        }
        return message;
    }


    @ApiOperation(value = "角色列表")
    @PostMapping("list")
    @ResponseBody
    public JsonMessage list(Authentication authentication, @RequestParam(defaultValue = "0") int pageIndex,
                            @RequestParam(defaultValue = "10") int pageSize) {
        JsonMessage message = new JsonMessage();
        AuthUserDetail detail = (AuthUserDetail) authentication.getDetails();

        PageInfo<Role> page = sysRoleService.list(pageIndex, pageSize);
        message.setData(page);
        return message;
    }

    @ApiOperation(value = "新建角色")
    @PostMapping("add")
    @ResponseBody
    public JsonMessage add(@RequestBody Role role) {
        JsonMessage message = new JsonMessage();
        sysRoleService.Insert(role);
        return message;
    }

    @ApiOperation(value = "删除角色")
    @PostMapping("delete")
    @ResponseBody
    public JsonMessage delete(String RoleId) {
        JsonMessage message = new JsonMessage();
        sysRoleService.Delete(RoleId);
        return message;
    }
}
